package t4invoke;

import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import t4dao.MoneyRecordDao;
import t4bean.FruitBean;
import t4bean.MoneyRecordBean;
import t4dao.FruitDao;

public class SellAction implements Action{

	@Override
	public void sell(Scanner sc) {
		System.out.println("输入水果种类");
		String fruitName=sc.nextLine();
		System.out.println("输入水果数量");
		Integer number= Integer.parseInt(sc.nextLine());
		System.out.println("输入顾客男女,男为1，女为0");
		Integer sex=Integer.parseInt(sc.nextLine());
		LocalDateTime t=LocalDateTime.now();
		int number2=number;
		if(t.getHour()>=17) {
			number=number/2;
		}
		if(sex==0) {
			number=number*8/10;
		}
		FruitBean fruit=new FruitBean();
		fruit.setName(fruitName);
		FruitDao fd=new FruitDao();
		
		List<FruitBean> list=new ArrayList<>();
		try {
			list=fd.select(fruit);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		if(list.size()==0) {
			System.out.println("水果不存在");
			return;
		}
//		System.out.println(list.size());
		FruitBean fruit2=list.get(0);
		
		if(fruit2.getNumber()<number2) {
			System.out.println("水果数量不足");
			return;
		}
		fruit2.setNumber(fruit2.getNumber()-number2);
		fd.update(fruit2);
		if(number==0) {
			number=1;
		}
		
		Integer money=number*fruit2.getPrice();
		System.out.println("花费了"+money);
		
		
		MoneyRecordBean mrb=new MoneyRecordBean();
		mrb.setFruitName(fruitName);
		mrb.setNumber(number2);
		mrb.setSellingTime(t);
		mrb.setMoney(money);
		mrb.setCost(fruit2.getCost());
		MoneyRecordDao mrd=new MoneyRecordDao();
		mrd.insert(mrb);
	}

	@Override
	public void purchase(Scanner sc) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void moneyGet(Scanner sc) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void showAllSellRecord() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void three() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void showFruit() {
		// TODO Auto-generated method stub
		
	}
	
}
